package com.example.springboot_admin.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot_admin.pojo.Categories;
import com.example.springboot_admin.service.ICategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author svblack
 * @since 2024-09-14
 */
@RestController
@RequestMapping("/categories")
public class CategoriesController {
    @Autowired
    ICategoriesService categoriesService;

    @RequestMapping("list")
    IPage<Categories> list(@RequestParam(defaultValue = "1") int page,
                      @RequestParam(defaultValue = "5")int limit){
        return categoriesService.page(new Page<>(page,limit));
    }

    @RequestMapping("del")
    Integer del(int categoryid){
        return categoriesService.removeById(categoryid)?0:1;
    }

    @RequestMapping("doAdd")
    Integer doAdd(@RequestBody Categories categories){
        return categoriesService.save(categories)?0:1;
    }

    @RequestMapping("delMult")
    Integer delMult(@RequestBody List<Integer> ids){
        System.out.println("ids:"+ids);
        categoriesService.removeByIds(ids);
        return 0;
    }

    @RequestMapping("doEdit")
    Integer doEdit(@RequestBody Categories categories){
        UpdateWrapper<Categories> wrapper = new UpdateWrapper<>();
        if(categories.getParent_id()!=null){
            wrapper.set("parent_id", categories.getParent_id());
        }
        if(categories.getName()!=null&&!categories.getName().isEmpty()){
            wrapper.set("name", categories.getName());
        }
        wrapper.eq("category_id", categories.getCategory_id());
        return categoriesService.update(wrapper)?0:1;
    }
}

